package cn.sc.summer.exception.handler;

import cn.sc.summer.exception.exceptions.CommonException;
import cn.sc.summer.exception.exceptions.FeignException;
import cn.sc.summer.constant.model.Result;
import cn.sc.summer.exception.exceptions.AssertException;
import cn.sc.summer.exception.exceptions.NotFindGirlFriendException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 类名：全局异常捕获
 *
 * @author a-xin
 * @date 2:29 PM
 */
@ControllerAdvice
public class ExceptionHandler {

    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class)
    public Result<String> exceptionResult(RuntimeException e) {
        e.printStackTrace();
        return Result.fail(e.getMessage(), e.getMessage());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @org.springframework.web.bind.annotation.ExceptionHandler(value = CommonException.class)
    private Result<String> customExceptionHandler(CommonException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @org.springframework.web.bind.annotation.ExceptionHandler(value = NotFindGirlFriendException.class)
    private Result<String> customExceptionHandler(NotFindGirlFriendException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @org.springframework.web.bind.annotation.ExceptionHandler(value = FeignException.class)
    private Result<String> customExceptionHandler(FeignException e) {
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @org.springframework.web.bind.annotation.ExceptionHandler(value = AssertException.class)
    private Result<String> customExceptionHandler(AssertException e) {
        return Result.fail(e.getCode(), e.getMessage());
    }

}
